<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/8/14-15:57
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Article.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */


    namespace app\index\controller;

    use think\Cache;
    use think\Exception;

    class Article extends Base
    {


        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
        }

        /**
         * @return mixed
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: index
         * @describe:
         */
        public function index(){

            return $this->fetch();
        }


        /**
         * @return mixed
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: lists
         * @describe:文章列表页
         */
        public function lists(){
            $info=model("navigate")->get($this->navigate_id);
            if(!$info){
                $this->redirect("lucky/notfound");
            }
            $data=$this->article_model->indexGetArticle(['navigate_id'=>$this->navigate_id],10);

            $this->right_article(['navigate_id'=>$this->navigate_id]);

            $page = $data->render();
            $this->assign('datas',$data);
            $this->assign('page',$page);
            return $this->fetch();
        }


        /**
         * @return mixed
         * @throws \think\Exception
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: show
         * @describe:文章详情页
         */
        public function show(){


            $id=$this->request->param("id/d",0,"intval");

            if(empty($id)|| $id==0){
                $this->redirect("lucky/notfound");
            }


            $info=$this->article_model->get($id);
            if(!$info) {
                $this->redirect("lucky/notfound");
            }

            $this->right_article(['navigate_id'=>$this->navigate_id,'id'=>['neq',$id]]);

            $comment=model("admin/Comment")->admin_article_comments($id); //文章评论

            if(!empty($comment)){
                $this->assign("page",$comment->render());
            }
            $this->assign("comment",$comment); //评论列表


            $this->assign("count",count($comment)); //评论数量

            $this->assign("id",$id);
            $this->assign("data",$this->article_model->showarticle($id)); //文章详细


            return $this->fetch();

        }


        /**
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: comment
         * @describe:文章评论
         */
        public function comment(){
            if($this->request->isPost()){
              $data=$this->request->post();

                //不需要审核
                if ((int)$this->setting['safe']['ping_check']==2){
                   $data['status']=1;
                }

               //用户检测
               $this->chek_user($data['qq']);

                //防止频繁留言
                if(Cache::has("article_comment"."_".$data['qq'])){
                    $this->error("请别频繁评论，谢谢！");
                }
              //dump($data);die;
              $data['content']=GetSensitive($data['content']);//过滤

              $res=model("admin/Comment")->save_data($data);
              if($res!=false){
                  $this->article_model->comment_nums($data['article_id']); //文章评论加一
                  Cache::set("article_comment"."_".$data['qq'],$data['content'],300); //5分钟
                  $this->success();
              }else{
                  $this->error("网络错误，请稍后重试！");
              }

            }
        }


        /**
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: comment_zan
         * @describe:评论点赞
         */
        public function comment_zan(){
            if($this->request->isAjax()){
                $id=$this->request->post("id",0,"intval");
                if($id==0 || empty($id)){
                    $this->error("网络错误");
                }
                $res=model("admin/Comment")->ZanComment($id);
                $this->success("谢谢亲的点赞");
            }
        }




        /**
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: like
         * @describe:文章点赞喜欢
         */
        public function like(){
            if($this->request->isAjax()){
                $id=$this->request->post("id",0,"intval");
                if(empty($id) || $id==0){
                    $this->error("网络错误，请稍后重试！");
                }
                try{
                    $res=db($this->model_data['tablename'])->where("id",$id)->setInc("zan");
                    if($res) {
                        $this->success("谢谢你的点赞！");
                    }else{
                        $this->error("点赞失败！");
                    }
                }catch (Exception $exception){
                    $this->error($exception->getMessage());
                }
            }
        }



    }